package com.lbd.smsplatform.controller;

import com.lbd.base.controller.BaseController;
import com.lbd.commons.Result;
import com.lbd.commons.ResultState;
import com.lbd.smsplatform.bean.SmsWhiteList;
import com.lbd.smsplatform.service.SmsWhiteListService;
import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Arrays;

/**
 * Created by wang on 2016/9/23.
 */
@RestController
@RequestMapping("/smsWhiteList")
public class SmsWhiteListController extends BaseController {
    private final Logger log = Logger.getLogger(this.getClass());

    @Autowired
    SmsWhiteListService smsWhiteListService;

    /**
     * 1. 新增短信规则
     *
     * @param smsWhiteList 短信规则bean
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result add(@Valid SmsWhiteList smsWhiteList) {
        smsWhiteListService.save(smsWhiteList);
        return new Result(ResultState.SUCCESS, "新增成功！");

    }

    /**
     * 2. 短信规则列表
     *
     * @param page         页码
     * @param rows         条数
     * @param smsWhiteList 短信规则bean
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result list(int page, int rows, SmsWhiteList smsWhiteList) {
        return new Result(ResultState.SUCCESS, "查询成功", smsWhiteListService.findByPageConditionJpql(page, rows, smsWhiteList));
    }

    /**
     * 3. 短信规则删除
     *
     * @param ids 短信规则id数组
     * @return
     */
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public Result del(@RequestParam("ids[]") String[] ids) {
        System.out.println(Arrays.toString(ids));
        smsWhiteListService.del(ids);
        return new Result(ResultState.SUCCESS, "删除成功", "");

    }

    /**
     * 4. 短信规则编辑
     *
     * @param id 短信规则id
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public Result toEdit(String id) {
        return new Result(ResultState.SUCCESS, "加载成功", smsWhiteListService.findById(id));

    }

    /**
     * 5. 短信规则修改
     *
     * @param smsWhiteList 短信规则bean
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public Result update(SmsWhiteList smsWhiteList) {
        smsWhiteListService.update(smsWhiteList);
        return new Result(ResultState.SUCCESS, "更新成功");

    }


}
